<?php

namespace Core\Helper;


use Core\Exception\BusinessException;
use Core\Exception\SystemException;

class ApplicationDirectoryHelper{

    public static  function getAllTemplateFile()
    {
        $result = array();
        $dir = CORE_TEMPLATE;
        $tmpArr = glob($dir .'/'. "tpl*");
        foreach ($tmpArr as $name) {
            array_push($result, basename($name, ".php"));
        }


        return $result;
    }


    public static function prepareViewScript($module, $controller, $action){
        $script = CORE_DOCUMENT_ROOT . '/application/modules' . '/' . $module . '/view/'.$module.'/' . $controller . '/' . $action . '.php';
        $destFile = CORE_COMMON_TEMPLATE.'/tmp/'.$action.'.php';
        if(file_exists($script)){
            if (!copy($script, $destFile)) {
                throw new BusinessException("Copy View scrip lỗi!");
            }
            return $script;
        }

        else throw new BusinessException("Action scrip không tồn tại. Kiểm tra lại /module/controller/action!");

    }

    public static function cleanTmpScriptFolder(){
        $dir =  CORE_COMMON_TEMPLATE.'/tmp/';
        foreach(glob($dir.'*.*') as $v){
            unlink($v);
        }
    }

    public static function getAllProgramCode()
    {
        $excludeArr = array('Abstract', 'Factory');
        $result = array();
        $dir = LIB_PATH . "/Core/Widget";
        $tmpArr = glob($dir .'/'. "*.php");

        foreach ($tmpArr as $name) {
            $className = substr(basename($name),0, strlen(basename($name)) - 4);

            if(!in_array($className, $excludeArr))

                array_push($result, array('text'=>$className, 'value'=>$className));
        }
        return $result;
    }

    public static function getAllModule()
    {
        $result = array();
        $dir = MODULE_PATH;
        $tmpArr = glob($dir . '/*' , GLOB_ONLYDIR);
        foreach ($tmpArr as $name) {
            array_push($result, basename($name));
        }
        return $result;
    }

    public static function getAllModuleForAdmin()
    {
        $excludeModules = array('admin', 'block','common', 'demo');
        $result = array();
        $dir = MODULE_PATH;
        $tmpArr = glob($dir . '/*' , GLOB_ONLYDIR);
        foreach ($tmpArr as $name) {
            if(!in_array(basename($name), $excludeModules))
                array_push($result, basename($name));
        }
        return $result;
    }

    public static function getAllControllerByModule($module)
    {
        $result = array();
        $dir = MODULE_PATH.'/'.$module.'/src/'.$module.'/Controller';
        $tmpArr = glob($dir .'/'. "*.php");
        foreach ($tmpArr as $name) {
            $fullName = basename($name, ".php");
            $myName =  strtolower(str_replace('Controller', '', $fullName));
            array_push($result, $myName);
        }
        return $result;
    }
}

